#include <mod/include/LM92.h>

/***********************************************************************************************************************
*
*        LM92 = Temperatuur sensor.
*
*        Versie 0.1  28/05/2010
*        Opstellen LM92 programma.
*
*        Versie 0.2  02/06/2010
*        Uitbreiden.
*
*
***********************************************************************************************************************/



//------------------------------------------- Functies -------------------------------------------
void LM92_Init(void)
{ 
  unsigned int loop;
  unsigned char adresses[2]={0x01,0x04};
  loop=500;
//  TWI_Start1(0x4B,0,&adresses,2);               //Slave select  laatste 4 bits: 0 A1 A2 0(RW)             Testen met: 1001 0000 (write)
  TWI_Start(0x4B,0);               //Slave select  laatste 4 bits: 0 A1 A2 0(RW)             Testen met: 1001 0000 (write)
  TWI_Write(0x01);               //Aanroepen van het Configuration Register.
  TWI_Write(0x04);               //Instellen van het Configuration Register.
  TWI_Stop();                    //Stop de verbinding, LM92 is ingesteld.
  while(loop--);
  TWI_Start(0x4B,0);
  //TWI_Repeated_Start();
  TWI_Write(0x00);               //Aanroepen van het Configuration Register.
/*  TWI_Write(0x02);               //Aanroepen van THYST
  TWI_Write(0x??);               //Instellen van THYST  
  TWI_Write(0x03);               //Aanroepen van T_CRIT
  TWI_Write(0x??);               //Instellen van T_CRIT  
  TWI_Write(0x04);               //Aanroepen van TLOW
  TWI_Write(0x??);               //Instellen van TLOW  
  TWI_Write(0x05);               //Aanroepen van THIGH
  TWI_Write(0x??);               //Instellen van THIGH  */
  TWI_Stop();                    //Stop de verbinding, LM92 is ingesteld.
}

int LM92_Read(void)
{
  static double avg[10]={0,0,0,0,0,0,0,0,0,0};
  double som;
  long int Rdata,Rdata1,Temp;
  double temperature;
//  TWI_Start(0x4B,0);               //Slave select  laatste 4 bits: 0 A1 A2 0(RW)                              Nog aanpassen!
//  TWI_Write(0x00);               //Aanroepen van het Temperature Register.
  TWI_Start(0x4B,1);
  Rdata = TWI_Read();
//  while(Rdata == -1){
//     Rdata = TWI_Read();
//  }
//  Temp = Rdata<<8;
  Temp = Rdata;
  Rdata1 = TWI_Read();
//  while(Rdata == -1){
//     Rdata = TWI_Read();
//  }
  TWI_Stop();
  Temp |= (Rdata1<<8);
  Temp >>= 3;
  Temp &= 0x1FFF;
  temperature=Temp;
  temperature*=0.0625;
  if(temperature>255.0){
     TWI_Reset();
     TWI_Init();
     LM92_Init();
     TWI_Start(0x4B,1);
     Rdata = TWI_Read();
     Temp = Rdata;
     Rdata1 = TWI_Read();
     TWI_Stop();
     Temp |= (Rdata1<<8);
     Temp >>= 3;
     Temp &= 0x1FFF;
     temperature=Temp;
     temperature*=0.0625;
  }
  som=0;
  for(Rdata=0;Rdata<9;Rdata++){
     avg[Rdata]=avg[Rdata+1];
     som+=avg[Rdata];
  }
  som+=temperature;
  avg[9]=temperature;
//  Temp=som/10.0;
  
  if(Temp < 0xFFFF){
     return(Temp);
  }else{
     return(0);
  }
}

void LM92_Stop(void)
{
  TWI_Start(0x4B,0);               //Slave select  laatste 4 bits: 0 A1 A2 0(RW)        !!!4B!!!                            Nog aanpassen!
  TWI_Write(0x01);               //Aanroepen van het Configuration Register.
  TWI_Write(0x01);               //Uitzetten van de sensor(power down mode).
  TWI_Stop();
}


//1 = 0,0625





